<?php

class nearbydish{
public function index(){
$city_id = intval($GLOBALS['request']['city_id']);
$cate_id = intval($GLOBALS['request']['cate_id']);
$cate_type_id = intval($GLOBALS['request']['cate_type_id']);
$ordertype=$GLOBALS['request']['order_by_type'];
$root=array();
$page = intval($GLOBALS['request']['page']);
$page=$page==0?1:$page;
$page_size = PAGE_SIZE;
$limit = (($page-1)*$page_size).','.$page_size;
$distance = intval($GLOBALS['request']['distance']);
$key_word=trim($GLOBALS['request']['key_word']);
$lat = trim($GLOBALS['request']['log']);
$log =trim($GLOBALS['request']['lat']);
$around=getAround($lat,$log,$distance);
$order_type=get_order_type($ordertype);
if(empty($key_word)){
$where=" sl.city_id='{$city_id}' AND deal_type=0 AND sl.xpoint > '{$around['minLat']}' AND sl.xpoint < '{$around['maxLat']}' AND sl.ypoint > '{$around['minLng']}' AND sl.ypoint < '{$around['maxLng']}'  ";
if(empty($cate_type_id))
{
$from=' '.DB_PREFIX.'supplier_location_menu as slm left join '.DB_PREFIX.'supplier_location as sl on slm.supplier_location_id=sl.id ';
$sql_count = "SELECT COUNT(slm.id)   FROM {$from}   where {$where}   ";
$sql="SELECT slm.id as menu_id,slm.supplier_location_id as supplier_id,slm.name as menu_name,slm.price,slm.preview,sl.name as supplier_name,sl.xpoint,sl.ypoint,slm.collect_nums,sl.menu_count  FROM {$from}  where {$where} order by sl.{$order_type} limit ".$limit;
}else{
$sql_count = 'SELECT COUNT(dctm.menu_id)   FROM ('.DB_PREFIX.'deal_cate_type_menu_link as dctm left join '.DB_PREFIX.'supplier_location_menu as slm on slm.id=dctm.menu_id) left join '.DB_PREFIX."supplier_location as sl on sl.id=slm.supplier_location_id  where {$where} and deal_cate_type_id='{$cate_type_id}'  ";
$sql='SELECT dctm.menu_id,slm.supplier_location_id as supplier_id,slm.name as menu_name,slm.price,slm.preview,sl.name as supplier_name,sl.xpoint,sl.ypoint,slm.collect_nums,sl.menu_count  FROM ('.DB_PREFIX.'deal_cate_type_menu_link as dctm left join '.DB_PREFIX.'supplier_location_menu as slm on slm.id=dctm.menu_id) left join '.DB_PREFIX."supplier_location as sl on sl.id=slm.supplier_location_id  where {$where} and deal_cate_type_id='{$cate_type_id}' order by sl.{$order_type} limit ".$limit;
}
}else{
$where=" sl.city_id='{$city_id}' AND deal_type=0 AND sl.xpoint > '{$around['minLat']}' AND sl.xpoint < '{$around['maxLat']}' AND sl.ypoint > '{$around['minLng']}' AND sl.ypoint < '{$around['maxLng']}'  ";
if(empty($cate_type_id))
{
$from=' '.DB_PREFIX.'supplier_location_menu as slm left join '.DB_PREFIX.'supplier_location as sl on slm.supplier_location_id=sl.id ';
$sql_count = "SELECT COUNT(slm.id)   FROM {$from}   where {$where} and slm.name like '%".$key_word."%'   ";
$sql="SELECT slm.id as menu_id,slm.supplier_location_id as supplier_id,slm.name as menu_name,slm.price,slm.preview,sl.name as supplier_name,sl.xpoint,sl.ypoint,slm.collect_nums,sl.menu_count  FROM {$from}  where {$where} and slm.name like '%".$key_word."%' order by sl.{$order_type} limit ".$limit;
}else{
$sql_count = 'SELECT COUNT(dctm.menu_id)   FROM ('.DB_PREFIX.'deal_cate_type_menu_link as dctm left join '.DB_PREFIX.'supplier_location_menu as slm on slm.id=dctm.menu_id) left join '.DB_PREFIX."supplier_location as sl on sl.id=slm.supplier_location_id  where {$where} and slm.name like '%".$key_word."%' and deal_cate_type_id='{$cate_type_id}'  ";
$sql='SELECT dctm.menu_id,slm.supplier_location_id as supplier_id,slm.name as menu_name,slm.price,slm.preview,sl.name as supplier_name,sl.xpoint,sl.ypoint,slm.collect_nums,sl.menu_count  FROM ('.DB_PREFIX.'deal_cate_type_menu_link as dctm left join '.DB_PREFIX.'supplier_location_menu as slm on slm.id=dctm.menu_id) left join '.DB_PREFIX."supplier_location as sl on sl.id=slm.supplier_location_id  where {$where} and slm.name like '%".$key_word."%' and deal_cate_type_id='{$cate_type_id}' order by sl.{$order_type} limit ".$limit;
}
}
$count=$GLOBALS['db']->getOne($sql_count);
$list=  $GLOBALS['db']->getAll($sql);
foreach($list as $key=>$value)
{
$list[$key]['distance']=GetDistance($lat,$log,$value['xpoint'],$value['ypoint']);
$list[$key]['price']='￥'.round($value['price'],1);
if(empty($value['preview'])){
$list[$key]['image']='';
}else{
$list[$key]['image']=get_abs_img_root(get_spec_image($value['preview'],400,240,1));
}
unset($list[$key]['preview'],$list[$key]['xpoint'],$list[$key]['ypoint']);
}
$page_total = ceil($count/$page_size);
$root['item'] =$list;
$root['page'] = array('page'=>$page,'page_total'=>$page_total);
output($root);
}
}
?>